<?php

namespace app\Home\controller;

use app\common\controller\Home;

class Team extends Home
{
    protected $CommissionModel = null;
    protected $OrderModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->CommissionModel = model('Business.Commission');
        $this->OrderModel = model('Business.Order');
    }

    // 推广海报
    public function poster()
    {
        // 获取当前头像和海报
        $avatar = isset($this->view->AutoLogin['avatar']) ? trim($this->view->AutoLogin['avatar']) : '';
        $poster = isset($this->view->AutoLogin['poster']) ? trim($this->view->AutoLogin['poster']) : '';

        // 判断头像是否为图片
        $avatar = is_file("." . $avatar) ? "." . $avatar : "";

        // 获取当前域名
        $domain = $this->request->domain();

        // 身份识别 id+mobile 进行md5
        $id = isset($this->view->AutoLogin['id']) ? trim($this->view->AutoLogin['id']) : 0;
        $mobile = isset($this->view->AutoLogin['mobile']) ? trim($this->view->AutoLogin['mobile']) : '';
        $token = md5($id . $mobile);

        // 推广链接
        // http://www.fast.com/home/index/register?busid
        $url = "$domain/home/index/register?token=$token";

        // 判断海报是否存在，如果不存在则生成一张海报
        if ($this->request->isAjax()) {
            //生成海报
            $qrcode = new \dh2y\qrcode\QRcode();

            //生成二维码图片
            $level = ['L', 'M', 'Q', 'H'];
            $index = array_rand($level);

            $path = $qrcode->png($url, false, 5, $level[$index])    # 生成二维码
                ->logo($avatar)     # 生成logo二维码
                ->getPath();    # 获取二维码生成的地址

            //更新到数据库中去
            $data = [
                'id' => $this->view->AutoLogin['id'],
                'poster' => $path
            ];

            //更新
            $result = $this->BusinessModel->isUpdate(true)->save($data);

            if ($result === FALSE) {
                $this->error('更新海报失败');
                exit;
            } else {
                //判断海报是否存在,如果存在就删除旧图片
                @is_file("." . $poster) && @unlink("." . $poster);

                //返回数据
                $this->success('更新个人海报成功', url('home/team/poster'), ['poster' => $path]);
                exit;
            }
        }

        //赋值数据
        $this->assign('url', $url);

        return $this->view->fetch();
    }


    // 我的团队
    public function team()
    {
        $list = $this->BusinessModel->where(['parentid' => $this->view->AutoLogin['id']])->select();

        $this->assign('list', $list);

        return $this->view->fetch();
    }

    // 提现
    public function money()
    {
        // 链表查询
        $commis = $this->CommissionModel->with(['busorder', 'bus'])->select();
        $order = $this->OrderModel->with(['subject'])->select();

        // var_dump($order->toArray());
        // exit;

        $this->assign([
            'commis' => $commis,
            'order' => $order
        ]);

        return $this->view->fetch();
    }
}
